/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Responses.DataRetrievalComponents;

import com.sun.xml.ws.commons.virtualbox_3_2.IHost;
import java.util.Date;
import java.util.EnumMap;
import org.virtualbox_3_2.ProcessorFeature;

/**
 * This class represents system host data, when retrieved from the system.
 *
 * @author Angel Sanadinov
 */
public class SystemHostData extends DataRetrievalComponent
{
    private int processorsCount;                //number of processors on the host
    private int onlineProcessorsCount;          //number of online processors
    private int processorCoresCount;            //number of processor cores

    private int memorySize;                     //host memory size
    private int availableMemory;                //available memory
    private String operatingSystem;             //host operating system
    private String operatingSystemVersion;      //host operating system version
    private Date UTCTime;                       //host time in UTC
    private boolean acceleration3DAvailable;  //denotes whether 3D acceleration is available

    private int[] processorSpeeds;              //speeds for each processor
    private String[] processorDescriptions;     //descriptions for each procressor
    private EnumMap<ProcessorFeature, Boolean> processorFeatures;//host processor features

    /**
     * Constructs a system host data component, using the IHost data.
     *
     * @param host the host data
     */
    public SystemHostData(IHost host)
    {
        this.processorsCount = host.getProcessorCount().intValue();
        this.onlineProcessorsCount = host.getProcessorOnlineCount().intValue();
//        this.processorCoresCount = host.getProcessorCoreCount().intValue();
        this.memorySize = host.getMemorySize().intValue();
        this.availableMemory = host.getMemoryAvailable().intValue();
        this.operatingSystem = host.getOperatingSystem();
        this.operatingSystemVersion = host.getOSVersion();
        this.UTCTime = new Date(host.getUTCTime());
        this.acceleration3DAvailable = host.getAcceleration3DAvailable();
        
        this.processorSpeeds = new int[processorsCount];
        this.processorDescriptions = new String[processorsCount];
        for(int i = 0; i < processorsCount; i++)
        {
            this.processorSpeeds[i] = host.getProcessorSpeed(new Long(i)).intValue();
            this.processorDescriptions[i] = host.getProcessorDescription(new Long(i));
        }

        EnumMap<ProcessorFeature, Boolean> features = new EnumMap<ProcessorFeature, Boolean>(ProcessorFeature.class);
        for(ProcessorFeature processorFeature : ProcessorFeature.values())
            features.put(processorFeature, host.getProcessorFeature(processorFeature));

        this.processorFeatures = features;
    }

    /**
     * Retrieves the number of processors on the host.
     *
     * @return the number of processors
     */
    public int getProcessorsCount()
    {
        return processorsCount;
    }

    /**
     * Retrieves the number of online processors.
     *
     * @return the number of online processors
     */
    public int getOnlineProcessorsCount()
    {
        return onlineProcessorsCount;
    }

    /**
     * Retrieves the number of processor cores on the host.
     *
     * @return the number of processor cores
     */
    public int getProcessorCoresCount()
    {
        return processorCoresCount;
    }

    /**
     * Retrieves the host memory size.
     *
     * @return the memory size (in MBs)
     */
    public int getMemorySize()
    {
        return memorySize;
    }

    /**
     * Retrieves the amount of memory currently available on the host.
     *
     * @return the available memory size (in MBs)
     */
    public int getAvailableMemory()
    {
        return availableMemory;
    }

    /**
     * Retrieves the host operating system.
     *
     * @return the operating system
     */
    public String getOperatingSystem()
    {
        return operatingSystem;
    }

    /**
     * Retrieves the host operating system version
     *
     * @return the operating system version
     */
    public String getOperatingSystemVersion()
    {
        return operatingSystemVersion;
    }

    /**
     * Retrieves the host time, in UTC.
     *
     * @return the host time
     */
    public Date getUTCTime()
    {
        return UTCTime;
    }

    /**
     * Returns whether 3D acceleration is available on the host.
     *
     * @return <code>true</code> if 3D acceleration is available or <code>false</code>
     *         if it is not
     */
    public boolean isAcceleration3DAvailable()
    {
        return acceleration3DAvailable;
    }

    /**
     * Retrieves the speed of each processor.
     *
     * @return an array with the speeds of the host processors
     */
    public int[] getProcessorSpeeds()
    {
        return processorSpeeds;
    }

    /**
     * Retrieves the description of each processor.
     *
     * @return an array with the descriptions of the host processors
     */
    public String[] getProcessorDescriptions()
    {
        return processorDescriptions;
    }

    /**
     * Retrieves the processor(s) features.
     *
     * @return a map with processor features and if they are supported by the processor(s)
     */
    public EnumMap<ProcessorFeature, Boolean> getProcessorFeatures()
    {
        return processorFeatures;
    }

    @Override
    public boolean isValid()
    {
        if(processorSpeeds != null && processorDescriptions != null && processorFeatures != null
                && operatingSystem != null && operatingSystemVersion != null && memorySize > 0)
            return true;
        else
            return false;
    }
}
